import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

import Login from '@/views/Login.vue'
import Layout from '@/views/Layout.vue'
import Welcome from '@/views/main/Welcome.vue'
import Users from '@/views/main/Users.vue'
import Roles from '@/views/main/Roles.vue'
import Rights from '@/views/main/Rights.vue'
import Goods from '@/views/main/Goods.vue'
import Params from '@/views/main/Params.vue'
import Categories from '@/views/main/Categories.vue'
import Orders from '@/views/main/Orders.vue'
import Reports from '@/views/main/Reports.vue'
import Add from '@/views/main/Add.vue'
import store from '@/store'
const routes = [
  {
    path: '/login',
    component: Login,
  },
  {
    path: '/',
    component: Layout,
    redirect: '/welcome',
    children: [
      {
        path: 'welcome',
        component: Welcome,
      },
      {
        path: 'users',
        component: Users,
      },
      {
        path: 'roles',
        component: Roles,
      },
      {
        path: 'rights',
        component: Rights,
      },
      {
        path: 'goods',
        component: Goods,
      },
      {
        path: 'params',
        component: Params,
      },
      {
        path: 'categories',
        component: Categories,
      },
      {
        path: 'orders',
        component: Orders,
      },
      {
        path: 'reports',
        component: Reports,
      },
      {
        path: 'add',
        component: Add,
      },
    ],
  },
]

const router = new VueRouter({
  routes,
})

// 前置路由守卫
// const whiteList=['']
router.beforeEach((to, from, next) => {
  const token = store.state.user.token
  if (token) {
    if (to.path === '/login') {
      next('/')
    } else {
      next()
    }
  } else {
    if (to.path === '/login') {
      next()
    } else {
      next('/login')
    }
  }
})

export default router
